{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div style=\"color:#777777;background-color:#ffffff;font-size:12px;text-align:right;\">\n",
    "\tprepared by Abuzer Yakaryilmaz (QuSoft@Riga) | November 07, 2018\n",
    "</div>\n",
    "<table><tr><td><i> I have some macros here. If there is a problem with displaying mathematical formulas, please run me to load these macros.</i></td></td></table>\n",
    "$ \\newcommand{\\bra}[1]{\\langle #1|} $\n",
    "$ \\newcommand{\\ket}[1]{|#1\\rangle} $\n",
    "$ \\newcommand{\\braket}[2]{\\langle #1|#2\\rangle} $\n",
    "$ \\newcommand{\\inner}[2]{\\langle #1,#2\\rangle} $\n",
    "$ \\newcommand{\\biginner}[2]{\\left\\langle #1,#2\\right\\rangle} $\n",
    "$ \\newcommand{\\mymatrix}[2]{\\left( \\begin{array}{#1} #2\\end{array} \\right)} $\n",
    "$ \\newcommand{\\myvector}[1]{\\mymatrix{c}{#1}} $\n",
    "$ \\newcommand{\\myrvector}[1]{\\mymatrix{r}{#1}} $\n",
    "$ \\newcommand{\\mypar}[1]{\\left( #1 \\right)} $\n",
    "$ \\newcommand{\\mybigpar}[1]{ \\Big( #1 \\Big)} $\n",
    "$ \\newcommand{\\sqrttwo}{\\frac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\dsqrttwo}{\\dfrac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\onehalf}{\\frac{1}{2}} $\n",
    "$ \\newcommand{\\donehalf}{\\dfrac{1}{2}} $\n",
    "$ \\newcommand{\\hadamard}{ \\mymatrix{rr}{ \\sqrttwo & \\sqrttwo \\\\ \\sqrttwo & -\\sqrttwo }} $\n",
    "$ \\newcommand{\\vzero}{\\myvector{1\\\\0}} $\n",
    "$ \\newcommand{\\vone}{\\myvector{0\\\\1}} $\n",
    "$ \\newcommand{\\vhadamardzero}{\\myvector{ \\sqrttwo \\\\  \\sqrttwo } } $\n",
    "$ \\newcommand{\\vhadamardone}{ \\myrvector{ \\sqrttwo \\\\ -\\sqrttwo } } $\n",
    "$ \\newcommand{\\myarray}[2]{ \\begin{array}{#1}#2\\end{array}} $\n",
    "$ \\newcommand{\\X}{ \\mymatrix{cc}{0 & 1 \\\\ 1 & 0}  } $\n",
    "$ \\newcommand{\\Z}{ \\mymatrix{rr}{1 & 0 \\\\ 0 & -1}  } $\n",
    "$ \\newcommand{\\Htwo}{ \\mymatrix{rrrr}{ \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} } } $\n",
    "$ \\newcommand{\\CNOT}{ \\mymatrix{cccc}{1 & 0 & 0 & 0 \\\\ 0 & 1 & 0 & 0 \\\\ 0 & 0 & 0 & 1 \\\\ 0 & 0 & 1 & 0} } $\n",
    "$ \\newcommand{\\norm}[1]{ \\left\\lVert #1 \\right\\rVert } $"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2> <font color=\"blue\"> Solutions for </font>Probabilistic States</h2>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=\"task2\"></a>\n",
    "<h3> Task 2 </h3>\n",
    "\n",
    "Suppose that Fyodor hiddenly rolls a loaded (tricky) dice with the bias \n",
    "\n",
    "$$ Pr(1):Pr(2):Pr(3):Pr(4):Pr(5):Pr(6) = 7:5:4:2:6:1 . $$\n",
    "\n",
    "Represent your information on the result as a column vector. Remark that the size of your column should be 6.\n",
    "\n",
    "You may use python for your calculations."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3>Solution</h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "total portion is 25\n",
      "the weight of one portion is 0.04\n",
      "\n",
      "the probability of rolling 1 is 0.28\n",
      "the probability of rolling 2 is 0.2\n",
      "the probability of rolling 3 is 0.16\n",
      "the probability of rolling 4 is 0.08\n",
      "the probability of rolling 5 is 0.24\n",
      "the probability of rolling 6 is 0.04\n"
     ]
    }
   ],
   "source": [
    "# all portions are stored in a list \n",
    "all_portions = [7,5,4,2,6,1];\n",
    "\n",
    "# let's calculate the total portion\n",
    "total_portion = 0\n",
    "for i in range(6):\n",
    "    total_portion = total_portion + all_portions[i]\n",
    "\n",
    "print(\"total portion is\",total_portion)\n",
    "\n",
    "# find the weight of one portion\n",
    "one_portion = 1/total_portion\n",
    "print(\"the weight of one portion is\",one_portion)\n",
    "\n",
    "print() # print an empty line\n",
    "# now we can calculate the probabilities of rolling 1,2,3,4,5, and 6\n",
    "for i in range(6):\n",
    "    print(\"the probability of rolling\",(i+1),\"is\",(one_portion*all_portions[i]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=\"task4\"></a>\n",
    "<h3> Task 4 </h3>\n",
    "\n",
    "For a system with 4 states, randomly create a probabilistic state, and print its entries, e.g., $ 0.16~~0.17~~0.02~~0.65 $.\n",
    "\n",
    "You may pick your random numbers between 0 and 100 (or 1000), and then divide each by 100 (or 1000) to represent it as a probability."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3>Solution</h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# we will randomly create a probabilistic state\n",
    "#\n",
    "# we should be careful about two things:\n",
    "#     1. a probability value must be between 0 and 1\n",
    "#     2. the total probability must be 1\n",
    "#\n",
    "# therefore, we can randomly pick three probability values.\n",
    "# once we have three probability values, the fourth one is determined automatically\n",
    "# the fourth one cannot be arbitrary, because the summation of the four values must be 1\n",
    "\n",
    "# let's use a list of size 4\n",
    "# initial values are zeros\n",
    "my_state = [0,0,0,0]\n",
    "\n",
    "# we pick three random probabilistic values\n",
    "from random import randrange\n",
    "\n",
    "# I assume that I have the following total value to distribute to four parts\n",
    "total = 1000\n",
    "# I will randomly pick a value, and then continue with the remaining value\n",
    "for i in range(3): # let's find the three values\n",
    "    pick_a_value = randrange(total)\n",
    "    print(\"I picked\",pick_a_value)\n",
    "    my_state[i] = pick_a_value\n",
    "    total = total - pick_a_value # remaining value for the others\n",
    "my_state[3] = total # this is the remaining value after three iterations\n",
    "print(\"The remaining value is\",total)\n",
    "\n",
    "# let's verify the summation of the elements in my_state\n",
    "sum = 0\n",
    "print() # print an empty line\n",
    "for i in range(len(my_state)):\n",
    "    sum = sum + my_state[i]\n",
    "print(\"the summation of the elements in my_state is\",sum)\n",
    "\n",
    "# let's convert the selected values to the probabilities\n",
    "# we can also call this procedure as **NORMALIZATION**\n",
    "for i in range(len(my_state)):\n",
    "    my_state[i] = my_state[i]/1000\n",
    "\n",
    "print() # print an empty line\n",
    "print(\"the entries of my probabilistic state:\")\n",
    "# let's print all probabilities\n",
    "for i in range(len(my_state)): print(my_state[i])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
